//
//  ContentTableViewCell.m
//  XRSquare
//
//  Created by UaenaMo on 2018/7/26.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "ContentTableViewCell.h"

@interface ContentTableViewCell ()
{
    UILabel * titleLabel;

    UIImageView * content1Image;
    UIImageView * content2Image;
    UIImageView * content3Image;
    UIImageView * content4Image;

    UILabel * titleContent1;
    UILabel * titleContent2;
    UILabel * titleContent3;
    UILabel * titleContent4;

    UILabel * timeContent1;
    UILabel * timeContent2;
    UILabel * timeContent3;
    UILabel * timeContent4;
    
    UIView * gaview1;
    UIView * gaview2;
    UIView * gaview3;
    UIView * gaview4;

    UIButton * contentButton1;
    UIButton * contentButton2;
    UIButton * contentButton3;
    UIButton * contentButton4;

    NSArray * content;
}
@end

@implementation ContentTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIView * gray = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
        gray.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
        [self addSubview:gray];
    
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, ScreenWidth, 20)];
        titleLabel.font = [UIFont systemFontOfSize:16];
        titleLabel.userInteractionEnabled = YES;
        [self addSubview:titleLabel];
    
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleAction)];
        [titleLabel addGestureRecognizer:tap];
    
        
        
        content1Image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 50, ScreenWidth/2 - 15, ScreenWidth/2 - 15)];
        content1Image.contentMode = UIViewContentModeScaleAspectFill;
        content1Image.clipsToBounds = YES;
        [self addSubview:content1Image];
        
        content2Image = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2 + 5, 50, ScreenWidth/2 - 15, ScreenWidth/2 - 15)];
        content2Image.contentMode = UIViewContentModeScaleAspectFill;
        content2Image.clipsToBounds = YES;
        [self addSubview:content2Image];
        
        content3Image = [[UIImageView alloc]initWithFrame:CGRectMake(10, ScreenWidth/2 + 30, ScreenWidth/2 - 15, ScreenWidth/2 - 15)];
        content3Image.contentMode = UIViewContentModeScaleAspectFill;
        content3Image.clipsToBounds = YES;
        [self addSubview:content3Image];
        
        content4Image = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2 + 5, ScreenWidth/2 + 30, ScreenWidth/2 - 15, ScreenWidth/2 - 15)];
        content4Image.contentMode = UIViewContentModeScaleAspectFill;
        content4Image.clipsToBounds = YES;
        [self addSubview:content4Image];
        
        
        
        
        titleContent1 = [[UILabel alloc]initWithFrame:CGRectMake(content1Image.frame.origin.x, content1Image.frame.origin.y + content1Image.frame.size.height + 5, content1Image.frame.size.width, 20)];
        titleContent1.font = [UIFont systemFontOfSize:14];
        [self addSubview:titleContent1];
        
        titleContent2 = [[UILabel alloc]initWithFrame:CGRectMake(content2Image.frame.origin.x, content2Image.frame.origin.y + content2Image.frame.size.height + 5, content2Image.frame.size.width, 20)];
        titleContent2.font = [UIFont systemFontOfSize:14];
        [self addSubview:titleContent2];
        
        titleContent3 = [[UILabel alloc]initWithFrame:CGRectMake(content3Image.frame.origin.x, content3Image.frame.origin.y + content3Image.frame.size.height + 5, content3Image.frame.size.width, 20)];
        titleContent3.font = [UIFont systemFontOfSize:14];
        [self addSubview:titleContent3];
        
        titleContent4 = [[UILabel alloc]initWithFrame:CGRectMake(content4Image.frame.origin.x, content4Image.frame.origin.y + content4Image.frame.size.height + 5, content4Image.frame.size.width, 20)];
        titleContent4.font = [UIFont systemFontOfSize:14];
        [self addSubview:titleContent4];
        
        
        
        timeContent1 = [[UILabel alloc]initWithFrame:CGRectMake(content1Image.frame.origin.x + 5, content1Image.frame.size.height + content1Image.frame.origin.y - 20, content1Image.frame.size.width, 20)];
        timeContent1.font = [UIFont systemFontOfSize:12];
        timeContent1.textColor = [UIColor whiteColor];
        [self addSubview:timeContent1];
        {
//            gaview1 = [[UIView alloc]initWithFrame:timeContent1.frame];
            gaview1 = [[UIView alloc]initWithFrame:CGRectMake(timeContent1.frame.origin.x - 5, timeContent1.frame.origin.y, timeContent1.frame.size.width, timeContent1.frame.size.height)];
            [self insertSubview:gaview1 belowSubview:timeContent1];
            
            CAGradientLayer *gradientLayer = [CAGradientLayer layer];
            gradientLayer.frame = gaview1.bounds;  // 设置显示的frame
            gradientLayer.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];  // 设置渐变颜色
            //    gradientLayer.locations = @[@0.0, @0.2, @0.5];    // 颜色的起点位置，递增，并且数量跟颜色数量相等
            gradientLayer.startPoint = CGPointMake(0.5, 0);   //
            gradientLayer.endPoint = CGPointMake(0.5, 1);     //
            [gaview1.layer addSublayer:gradientLayer];
        }
        
        timeContent2 = [[UILabel alloc]initWithFrame:CGRectMake(content2Image.frame.origin.x + 5, content2Image.frame.size.height + content2Image.frame.origin.y - 20, content2Image.frame.size.width, 20)];
        timeContent2.font = [UIFont systemFontOfSize:12];
        timeContent2.textColor = [UIColor whiteColor];
        [self addSubview:timeContent2];
        {
//            gaview2 = [[UIView alloc]initWithFrame:timeContent2.frame];
            gaview2 = [[UIView alloc]initWithFrame:CGRectMake(timeContent2.frame.origin.x - 5, timeContent2.frame.origin.y, timeContent2.frame.size.width, timeContent2.frame.size.height)];
            [self insertSubview:gaview2 belowSubview:timeContent2];
            
            CAGradientLayer *gradientLayer = [CAGradientLayer layer];
            gradientLayer.frame = gaview2.bounds;  // 设置显示的frame
            gradientLayer.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];  // 设置渐变颜色
            //    gradientLayer.locations = @[@0.0, @0.2, @0.5];    // 颜色的起点位置，递增，并且数量跟颜色数量相等
            gradientLayer.startPoint = CGPointMake(0.5, 0);   //
            gradientLayer.endPoint = CGPointMake(0.5, 1);     //
            [gaview2.layer addSublayer:gradientLayer];
        }
        
        timeContent3 = [[UILabel alloc]initWithFrame:CGRectMake(content3Image.frame.origin.x + 5, content3Image.frame.size.height + content3Image.frame.origin.y - 20, content3Image.frame.size.width, 20)];
        timeContent3.font = [UIFont systemFontOfSize:12];
        timeContent3.textColor = [UIColor whiteColor];
        [self addSubview:timeContent3];
        {
//            gaview3 = [[UIView alloc]initWithFrame:timeContent3.frame];
            gaview3 = [[UIView alloc]initWithFrame:CGRectMake(timeContent3.frame.origin.x - 5, timeContent3.frame.origin.y, timeContent3.frame.size.width, timeContent3.frame.size.height)];
            [self insertSubview:gaview3 belowSubview:timeContent3];
            
            CAGradientLayer *gradientLayer = [CAGradientLayer layer];
            gradientLayer.frame = gaview3.bounds;  // 设置显示的frame
            gradientLayer.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];  // 设置渐变颜色
            //    gradientLayer.locations = @[@0.0, @0.2, @0.5];    // 颜色的起点位置，递增，并且数量跟颜色数量相等
            gradientLayer.startPoint = CGPointMake(0.5, 0);   //
            gradientLayer.endPoint = CGPointMake(0.5, 1);     //
            [gaview3.layer addSublayer:gradientLayer];
        }
        
        timeContent4 = [[UILabel alloc]initWithFrame:CGRectMake(content4Image.frame.origin.x + 5, content4Image.frame.size.height + content4Image.frame.origin.y - 20, content4Image.frame.size.width, 20)];
        timeContent4.font = [UIFont systemFontOfSize:12];
        timeContent4.textColor = [UIColor whiteColor];
        [self addSubview:timeContent4];
        {
//            gaview4 = [[UIView alloc]initWithFrame:timeContent4.frame];
            gaview4 = [[UIView alloc]initWithFrame:CGRectMake(timeContent4.frame.origin.x - 5, timeContent4.frame.origin.y, timeContent4.frame.size.width, timeContent4.frame.size.height)];
            [self insertSubview:gaview4 belowSubview:timeContent4];
            
            CAGradientLayer *gradientLayer = [CAGradientLayer layer];
            gradientLayer.frame = gaview4.bounds;  // 设置显示的frame
            gradientLayer.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];  // 设置渐变颜色
            //    gradientLayer.locations = @[@0.0, @0.2, @0.5];    // 颜色的起点位置，递增，并且数量跟颜色数量相等
            gradientLayer.startPoint = CGPointMake(0.5, 0);   //
            gradientLayer.endPoint = CGPointMake(0.5, 1);     //
            [gaview4.layer addSublayer:gradientLayer];
        }
        
        
        contentButton1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 50, ScreenWidth/2, ScreenWidth/2 + 30)];
        [contentButton1 addTarget:self action:@selector(button1Action) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:contentButton1];
        
        contentButton2 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2, 50, ScreenWidth/2, ScreenWidth/2 + 30)];
        [contentButton2 addTarget:self action:@selector(button2Action) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:contentButton2];
        
        contentButton3 = [[UIButton alloc]initWithFrame:CGRectMake(0, ScreenWidth/2 + 30, ScreenWidth/2, ScreenWidth/2 + 30)];
        [contentButton3 addTarget:self action:@selector(button3Action) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:contentButton3];
        
        contentButton4 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2, ScreenWidth/2 + 30, ScreenWidth/2, ScreenWidth/2 + 30)];
        [contentButton4 addTarget:self action:@selector(button4Action) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:contentButton4];
    
    }
    return self;
}

+(CGFloat)heightwithData:(NSArray *)data
{
    if ([data count] >= 3)
    {
        return ScreenWidth/2 + 80 + ScreenWidth/2 + 30;
    }
    else
    {
        return ScreenWidth/2 + 80;
    }
}

-(void)loadData:(NSArray *)data title:(NSString *)title
{
    content = data;
    
    titleLabel.text = title;
    
    content4Image.hidden = YES;
    titleContent4.hidden = YES;
    timeContent4.hidden = YES;
    contentButton4.hidden = YES;
    content3Image.hidden = YES;
    titleContent3.hidden = YES;
    timeContent3.hidden = YES;
    contentButton3.hidden = YES;
    content2Image.hidden = YES;
    titleContent2.hidden = YES;
    timeContent2.hidden = YES;
    contentButton2.hidden = YES;
    content1Image.hidden = YES;
    titleContent1.hidden = YES;
    timeContent1.hidden = YES;
    contentButton1.hidden = YES;
    gaview1.hidden = YES;
    gaview2.hidden = YES;
    gaview3.hidden = YES;
    gaview4.hidden = YES;

    switch ([data count]) {
        case 4:
        {
            content4Image.hidden = NO;
            titleContent4.hidden = NO;
            timeContent4.hidden = NO;
            contentButton4.hidden = NO;
            gaview4.hidden = NO;

            [content4Image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[data objectAtIndex:3]objectForKey:@"Pic"]]]];
            if ([[data objectAtIndex:3]objectForKey:@"TaskTitle"])
            {
                titleContent4.text = [[data objectAtIndex:3]objectForKey:@"TaskTitle"];
            }
            else
            {
                titleContent4.text = [[data objectAtIndex:3]objectForKey:@"Talent"];
            }
            timeContent4.text = [NSString stringWithFormat:@"%@-%@",[[data objectAtIndex:3]objectForKey:@"TaskBeginDay"],[[data objectAtIndex:3]objectForKey:@"TaskEndDay"]];

        }
        case 3:
        {
            content3Image.hidden = NO;
            titleContent3.hidden = NO;
            timeContent3.hidden = NO;
            contentButton3.hidden = NO;
            gaview3.hidden = NO;

            [content3Image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[data objectAtIndex:2]objectForKey:@"Pic"]]]];
            if ([[data objectAtIndex:2]objectForKey:@"TaskTitle"])
            {
                titleContent3.text = [[data objectAtIndex:2]objectForKey:@"TaskTitle"];
            }
            else
            {
                titleContent3.text = [[data objectAtIndex:2]objectForKey:@"Talent"];
            }
            timeContent3.text = [NSString stringWithFormat:@"%@-%@",[[data objectAtIndex:2]objectForKey:@"TaskBeginDay"],[[data objectAtIndex:2]objectForKey:@"TaskEndDay"]];

        }
        case 2:
        {
            content2Image.hidden = NO;
            titleContent2.hidden = NO;
            timeContent2.hidden = NO;
            contentButton2.hidden = NO;
            gaview2.hidden = NO;

            [content2Image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[data objectAtIndex:1]objectForKey:@"Pic"]]]];
            if ([[data objectAtIndex:1]objectForKey:@"TaskTitle"])
            {
                titleContent2.text = [[data objectAtIndex:1]objectForKey:@"TaskTitle"];
            }
            else
            {
                titleContent2.text = [[data objectAtIndex:1]objectForKey:@"Talent"];
            }
            timeContent2.text = [NSString stringWithFormat:@"%@-%@",[[data objectAtIndex:1]objectForKey:@"TaskBeginDay"],[[data objectAtIndex:1]objectForKey:@"TaskEndDay"]];
        }
        case 1:
        {
            content1Image.hidden = NO;
            titleContent1.hidden = NO;
            timeContent1.hidden = NO;
            contentButton1.hidden = NO;
            gaview1.hidden = NO;

            [content1Image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[data objectAtIndex:0]objectForKey:@"Pic"]]]];
            if ([[data objectAtIndex:0]objectForKey:@"TaskTitle"])
            {
                titleContent1.text = [[data objectAtIndex:0]objectForKey:@"TaskTitle"];
            }
            else
            {
                titleContent1.text = [[data objectAtIndex:0]objectForKey:@"Talent"];
            }
            timeContent1.text = [NSString stringWithFormat:@"%@-%@",[[data objectAtIndex:0]objectForKey:@"TaskBeginDay"],[[data objectAtIndex:0]objectForKey:@"TaskEndDay"]];
        }
        default:
            break;
    }
}

-(void)button1Action
{
    [self.delegate selectedContent:[content firstObject] title:self.indexPathCurrent];
}

-(void)button2Action
{
    [self.delegate selectedContent:[content objectAtIndex:1] title:self.indexPathCurrent];
}

-(void)button3Action
{
    [self.delegate selectedContent:[content objectAtIndex:2] title:self.indexPathCurrent];
}

-(void)button4Action
{
    [self.delegate selectedContent:[content objectAtIndex:3] title:self.indexPathCurrent];
}

-(void)titleAction
{
    [self.delegate selectContentIndex:self.contentindexPath.row];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
